﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlKhaiBaoChiSoTinhLuongThuong : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetRate();
            GetChinhSach();
            GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());
        }
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = dt.ExecuteSQl_int(str, "[K_InsertHRSalesLevel]", CommandType.StoredProcedure, ref error,
            new SqlParameter("@Rate", DropDT.SelectedValue.ToString()),
            new SqlParameter("@BAToanNhom", float.Parse(txtBAToanNhom.Text)),
            new SqlParameter("@DSCN", float.Parse(txtDSCN.Text)),
            new SqlParameter("@DSNCN", float.Parse(txtDSNCN.Text)),
            new SqlParameter("@DSCaNhom", float.Parse(txtDSCaNHom.Text)),
            new SqlParameter("@NhanhChuan", Int32.Parse(txtNhanhChuan.Text)),
            new SqlParameter("@PolicyID", DropChinhSach.SelectedValue.ToString()));
        if (i == 1)
        {
            uscMsgBox1.AddMessage("Chỉ số này đã tồn tại", uscMsgBox.enmMessageType.Error);
        }
        GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());
    }
    void GetRate()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetRateHRSales]", CommandType.StoredProcedure);
        DropDT.DataSource = db;
        DropDT.DataTextField = "Description";
        DropDT.DataValueField = "Rate";
        DropDT.DataBind();
    }
    void GetChinhSach()
    { 
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetChinhSach]", CommandType.StoredProcedure);
        DropChinhSach.DataSource = db;
        DropChinhSach.DataTextField = "PolicyName";
        DropChinhSach.DataValueField = "PolicyID";
        DropChinhSach.DataBind();
    }
    void GetHrSalesLevel(string PolicyID)
    {
        if (PolicyID != "PC000")
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetHRSalesLevel]", CommandType.StoredProcedure,
                 new SqlParameter("@PolicyID", PolicyID));
            GridView1.DataSource = db;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
        int i = dt.ExecuteSQl_int(str, "K_DeleteHRSalesLevelPolicy", CommandType.StoredProcedure, ref error,
            new SqlParameter("@Rate", keys),
            new SqlParameter("@PolicyID", DropChinhSach.SelectedValue.ToString()));
        if (i != 0)
        {
            uscMsgBox1.AddMessage("Không tồn tại chỉ số này", uscMsgBox.enmMessageType.Error);
        }
        GridView1.EditIndex = -1;
        GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();

            TextBox txtGridBAToanNhom = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGridBAToanNhom");
            TextBox txtGridDSCN = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGridDSCN");
            TextBox txtGridDSNCN = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGridDSNCN");
            TextBox txtGridDSCaNhom = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGridDSCaNhom");
            TextBox txtGridNhanhChuan = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGridNhanhChuan");

            int i = dt.ExecuteSQl_int(str, "K_UpdateHRSalesLevelPolicy", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Rate", Int32.Parse(keys)),
                new SqlParameter("@BAToanNhom", float.Parse(txtGridBAToanNhom.Text)),
                new SqlParameter("@DSCN", float.Parse(txtGridDSCN.Text)),
                new SqlParameter("@DSNCN", float.Parse(txtGridDSNCN.Text)),
                new SqlParameter("@DSCaNhom", float.Parse(txtGridDSCaNhom.Text)),
                new SqlParameter("@NhanhChuan", Int32.Parse(txtGridNhanhChuan.Text)),
                new SqlParameter("@PolicyID", DropChinhSach.SelectedValue.ToString()));
            if (i != 0)
            {
                uscMsgBox1.AddMessage("Không tồn tại chỉ số này", uscMsgBox.enmMessageType.Error);
            }

            GridView1.EditIndex = -1;
            GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr ob = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            ob.ErrorLog(ex.Message, method);
        }

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());
    }
    protected void DropChinhSach_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetHrSalesLevel(DropChinhSach.SelectedValue.ToString());

    }
}
